import jsonp from 'jsonp';
import qs from 'qs';
const isProd = process.env.NODE_ENV === 'production';
const host = isProd ? 'https://service.youshang.com' : '/api';
const url1 = `${host}/commonservice/ajaxChecking.do`;
const url2 = `${host}/commonservice/register/registerForJdy.do`;

const jsonPHttp = function(url, params) {
  const qstr = qs.stringify(params);
  return new Promise((resolve, reject) => {
    jsonp(`${url}?${qstr}`, null, (err, data) => {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
};

export default {
  // 极验
  geetestChallenge() {
    return jsonPHttp(url1, {
      action: 'geetestChallenge'
    });
  },

  /* eslint-disable */
  // 发验证码
  geetestVerify({
    mobile,
    geetest_challenge,
    geetest_validate,
    geetest_seccode,
    sessionId
  }) {
    return jsonPHttp(url1, {
      action: 'geetestVerify',
      nextAction: 'sendYZJSMS',
      mobile,
      geetest_challenge,
      geetest_validate,
      geetest_seccode,
      sessionId
    });
  },

  // 校验手机号是否存在的接口
  checkMobileYZJAndJDYForRegister({ mobile }) {
    return jsonPHttp(url1, {
      action: 'checkMobileYZJAndJDYForRegister',
      mobile
    });
  },

  // 注册
  registerForJdy({ mobile, vcode }) {
    return jsonPHttp(url2, {
      vcode,
      mobile,
      name: `jdy${mobile}`,
      isDirectPassword: true,
      activeCode: '00723350',
      sourceFrom: '519',
      channelCode: '001040'
    });
  }
};
